import {_decorator, Component} from 'cc';
import {BaseInfo} from "db://assets/scripts/data/model/BaseInfo";
import {AudioManager} from "db://assets/scripts/common/AudioManager";
import {EffectManager} from "db://assets/scripts/common/EffectManager";
import {Skill} from "db://assets/scripts/data/model/Skill";
import {AudioPaths} from "db://assets/scripts/common/AudioPaths";
import {WarningCircleEffect} from "db://assets/scripts/game/effect/warningSkill/WarningCircleEffect";
import {WarningLineEffect} from "db://assets/scripts/game/effect/warningSkill/WarningLineEffect";
import {MonsterController} from "db://assets/scripts/game/role/monster/MonsterController";
import {WarningStripEffect} from "db://assets/scripts/game/effect/warningSkill/WarningStripEffect";

const {ccclass, property} = _decorator;

/**
 * 激光技能脚本
 */
@ccclass('LaserEffect')
export class LaserEffect extends Component {
    /**
     * 技能信息
     */
    public skill: Skill;
    /**
     * 基础信息
     */
    public baseInfo: BaseInfo;
    /**
     * 预警技能脚本
     */
    public warningScript: WarningCircleEffect | WarningLineEffect | WarningStripEffect;

    /**
     * 定时器
     */
    private timer: number;

    /**
     * 初始化
     * @param skill
     * @param baseInfo
     * @param parentScript
     */
    public init(skill: Skill, baseInfo: BaseInfo, parentScript?: MonsterController) {
        this.skill = skill;
        this.baseInfo = baseInfo;
        this.node.active = false;
        this.closeTimer();

        this.timer = setTimeout(() => {
            if (!parentScript.getIsDie()) {
                AudioManager.getInstance().playSound(AudioPaths.laserSound);
                this.node.active = true;
                parentScript?.warningScript?.hideWarning();
                EffectManager.getInstance().playParticle(this.node, skill.speed, true, null, () => {
                    this.closeTimer();
                });
            } else {
                this.closeTimer();
            }
        }, 400)
    }

    /**
     * 关闭定时器
     */
    private closeTimer() {
        if (this.timer) {
            clearTimeout(this.timer);
            this.timer = null;
        }
    }
}